package cn.com.dhc.danlu.widget.home;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yiji.www.frameworks.utils.DensityUtils;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.StringUtil;

/**
 * User: Geek_Soledad(wuhaiyang@danlu.com)
 * Date: 2016-08-12
 * Time: 16:14
 * Description: .....
 */
public class PromotionView extends RelativeLayout implements View.OnClickListener {
    public static final int THRESHOLD_PRO = 3;
    private LinearLayout llPromotion;
    private LinearLayout llFold;
    private ImageView ivFold;

    private int hightLightColor;
    private int hightLightSize;

    private int shortHeight = 0;
    private int longHeight = 0;


    private boolean isCurrentFolding = false;
    private boolean isFold = true;
    private OnFoldListener onFoldListener;

    public PromotionView(Context context) {
        this(context, null);
    }

    public PromotionView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PromotionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.dl_promtion_richtext_style);
        hightLightColor = typedArray.getColor(R.styleable.dl_promtion_richtext_style_text_highlight_color, Color.RED);
        hightLightSize = typedArray.getDimensionPixelSize(R.styleable.dl_promtion_richtext_style_text_hightlight_size, 13);
        typedArray.recycle();
        initView();
    }

    private void initView() {
        View view = View.inflate(getContext(), R.layout.dl_promotion_view, this);
        llPromotion = (LinearLayout) view.findViewById(R.id.ll_promotion);
        llFold = (LinearLayout) view.findViewById(R.id.ll_fold);
        ivFold = (ImageView) view.findViewById(R.id.iv_fold);
        llFold.setOnClickListener(this);
    }

    public boolean isCurrentFolding() {
        return isCurrentFolding;
    }

    public void bindPromotion(List<String> promotionLists, boolean isFold) {
        if (isCurrentFolding) {
            return;
        }
        if (null == promotionLists || promotionLists.isEmpty()) {
            return;
        }
        //先移除之前的缓存数据
        llPromotion.removeAllViews();
        this.isFold = isFold;
        for (int i = 0; i < promotionLists.size(); i++) {
            String promotion = promotionLists.get(i);
            @SuppressLint("InflateParams")
            View itemView = LayoutInflater.from(getContext()).inflate(R.layout.dl_item_promotion, null);
            TextView textView = (TextView) itemView.findViewById(R.id.tv_prompt);
            textView.setText(StringUtil.hightLight(promotion, hightLightColor, hightLightSize));
            llPromotion.addView(itemView);
        }
        longHeight = DensityUtils.dip2px(getContext(), 23) * promotionLists.size();
        shortHeight = DensityUtils.dip2px(getContext(), 23) * THRESHOLD_PRO;
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) llPromotion.getLayoutParams();
        if (promotionLists.size() > THRESHOLD_PRO) {
            //计算 显示三条促销优惠消息 所占的高度
            llFold.setVisibility(VISIBLE);
            ivFold.setImageResource(isFold ? R.drawable.icon_fold_down : R.drawable.dl_icon_fold_up);
            layoutParams.height = isFold ? shortHeight : longHeight;
            layoutParams.bottomMargin = 0;
        } else {
            layoutParams.height = longHeight;
            layoutParams.bottomMargin = DensityUtils.dip2px(getContext(), 10);
            llFold.setVisibility(GONE);
        }
        llPromotion.setLayoutParams(layoutParams);
    }


    @Override
    public void onClick(View v) {
        if (CommonUtils.isFastClick()) {
            return;
        }
        foldAction();

    }

    /**
     * 折叠动画
     */
    private void foldAction() {
        isCurrentFolding = true;
        int startHeight;
        int targetHeight;
        if (isFold) {
            startHeight = shortHeight;
            targetHeight = longHeight;
        } else {
            startHeight = longHeight;
            targetHeight = shortHeight;
        }
        this.isFold = !isFold;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) llPromotion.getLayoutParams();
        lp.height = targetHeight;
        llPromotion.setLayoutParams(lp);
        ValueAnimator animator = ValueAnimator.ofInt(startHeight, targetHeight);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) llPromotion.getLayoutParams();
                lp.height = animatedValue;
                llPromotion.setLayoutParams(lp);
            }
        });
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                isCurrentFolding = false;
                ViewParent viewParent = getParent();
                //降低耦合度
                if (null != viewParent && viewParent instanceof ViewGroup) {
                    ViewGroup parent = (ViewGroup) viewParent;
                    ImageView iv_fold = (ImageView) parent.findViewById(R.id.iv_fold);
                    if (null != iv_fold) {
                        iv_fold.setImageResource(isFold ? R.drawable.icon_fold_down : R.drawable.dl_icon_fold_up);
                    }
                }
                if (null != onFoldListener) {
                    onFoldListener.onFoldAction(isFold);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.setDuration(300);
        //执行动画之前先清除所有相关动画
        clearAnimation();
        animator.start();
    }

    public void setOnFoldListener(OnFoldListener onFoldListener) {
        this.onFoldListener = onFoldListener;
    }

    public interface OnFoldListener {
        void onFoldAction(boolean fold);
    }

}
